package com.dongdongshop.service.impl;

import com.dongdongshop.data.Page;
import com.dongdongshop.mapper.TbContentMapper;
import com.dongdongshop.pojo.TbContent;
import com.dongdongshop.pojo.TbContentCategory;
import com.dongdongshop.service.ContentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@DubboService
@Service
public class ContentServiceImpl implements ContentService {
    @Autowired
    private TbContentMapper tbContentMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Page<TbContent> findList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbContent> list = tbContentMapper.selectByExample(null);
        PageInfo<TbContent> pageInfo = new PageInfo<>(list);
        Page<TbContent> page = new Page<>(pageInfo.getPageNum(),pageInfo.getSize(),pageInfo.getPages(),pageInfo.getList());
        return page;
    }

    @Override
    public void addInfo(TbContent tbContent) {
        tbContentMapper.insertSelective(tbContent);
    }

    @Override
    public TbContent selectById(TbContent tbContent) {
        return tbContentMapper.selectById(tbContent);
    }

    @Override
    public void updateInfo(TbContent tbContent) {
        tbContentMapper.updateByPrimaryKeySelective(tbContent);
    }

    @Override
    public void deleteInfo(Long[] id) {
        tbContentMapper.deleteById(id);
    }

    @Override
    public List<TbContent> selectInfo(TbContent tbContent) {
//        Long categoryId = tbContent.getCategoryId();
//        // 先判断redis中是否存在
//        String key = "content:";
//        String field = "categoryId:" + categoryId;
//        //如果存在，直接返回
//        HashOperations hashJedis = redisTemplate.opsForHash();
//        boolean b = hashJedis.hasKey(key,field);
//        if (b){
//            List<TbContent> result = (List<TbContent>) hashJedis.get(key,field);;
//            return result;
//        }else {
//            //不存在，在数据库中查询并增加到redis
//            List<TbContent> tbContents = tbContentMapper.selectByCid(tbContent);
//            hashJedis.put(key,field,tbContents);
//            return tbContents;
//        }
        return tbContentMapper.selectByCid(tbContent);
    }


}
